package com.sihan.framework.etm.controller;

import com.framework.model.ApiResult;
import com.framework.util.crypto.asymmetric.sm2.SM2Coder;
import com.framework.web.captcha.Captcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "其他组件")
@RestController
@PropertySource("classpath:application.yml")
public class HomeController {

  @Value("${sm2PrivateKeyFile}")
  private String sm2PrivateKeyFile;

  @Autowired
  private Captcha captcha;

  @ApiOperation("获取验证码")
  @GetMapping("/captcha")
  public void captcha() {
    captcha.render();
  }

  /**
   * 会跳转到登录页面
   * @return
   */
  @GetMapping("/login")
  @ResponseStatus(HttpStatus.UNAUTHORIZED)
  public ApiResult<?> login() {
    return ApiResult.fail("没有登录");
  }

  /**
   * 会跳转到强制修改密码页面
   * @return
   */
  @GetMapping("/initcpwd")
  public ApiResult<?> initcpwd() {
    return ApiResult.fail("请重新修改密码");
  }

  @GetMapping("/key")
  public ApiResult<?> getKey() {
    SM2Coder sm2 = new SM2Coder(sm2PrivateKeyFile);
    sm2.saveGenerateKeyPair();
    return ApiResult.success(sm2.getPublicKey());
  }
  
  @GetMapping("/permition")
  @ResponseStatus(HttpStatus.UNAUTHORIZED)
  public ApiResult<?> permitionFail() {
    return ApiResult.fail("没有权限");
  }

}
